﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeanEngine.Data.Linq.Language
{
    public sealed class MultiPartJoinToken : JoinToken
    {
        #region Constructor Members

        public MultiPartJoinToken()
        {
            this.JoinType = Language.JoinType.INNER;
            this.PredicateList = new List<IQueryToken>();
        }

        #endregion

        #region PropertyInfo Members

        public TableToken AssociationTable { get; set; }
        public JoinType AssociationJoinType { get; set; }
        public List<IQueryToken> AssociationPredicateList { get; set; }

        #endregion

        #region ILanguageToken Members

        public override string GetStatement()
        {
            var sql = this.AssociationJoinType.ToString() + " JOIN " + this.AssociationTable.Name + " " + this.AssociationTable.Alias + " ON ";
            foreach (var predicate in AssociationPredicateList)
            {
                sql += predicate.GetStatement() + " ";
            }

            sql += Environment.NewLine;
            sql += base.GetStatement();

            return sql;
        }

        #endregion
    }
}
